package com.zyf.lj.service.impl;

import com.zyf.lj.entity.JobApplication;
import com.zyf.lj.mapper.JobApplicationMapper;
import com.zyf.lj.service.JobApplicationService;
import com.zyf.lj.vo.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

/**
 * 职位申请服务实现类
 */
@Service
public class JobApplicationServiceImpl implements JobApplicationService {
    
    @Autowired
    private JobApplicationMapper jobApplicationMapper;
    
    @Override
    @Transactional
    public boolean submitApplication(JobApplication application) {
        // 设置创建时间和更新时间
        Date now = new Date();
        application.setCreateTime(now);
        application.setUpdateTime(now);
        application.setStatus(0); // 待处理
        application.setIsDeleted(0);
        
        // 插入数据
        int rows = jobApplicationMapper.insert(application);
        return rows > 0;
    }
    
    @Override
    @Transactional
    public boolean updateApplication(JobApplication application) {
        // 设置更新时间
        application.setUpdateTime(new Date());
        
        // 更新数据
        int rows = jobApplicationMapper.update(application);
        return rows > 0;
    }
    
    @Override
    public JobApplication getApplicationById(Integer id) {
        return jobApplicationMapper.selectById(id);
    }
    
    @Override
    @Transactional
    public boolean deleteApplicationById(Integer id) {
        int rows = jobApplicationMapper.deleteById(id);
        return rows > 0;
    }
    
    @Override
    public List<JobApplication> getAllApplications() {
        return jobApplicationMapper.selectAll();
    }
    
    @Override
    public List<JobApplication> getApplicationsByJobId(Integer jobId) {
        return jobApplicationMapper.selectByJobId(jobId);
    }
    
    @Override
    public List<JobApplication> getApplicationsByPhone(String phone) {
        return jobApplicationMapper.selectByPhone(phone);
    }
    
    @Override
    public List<JobApplication> getApplicationsByStatus(Integer status) {
        return jobApplicationMapper.selectByStatus(status);
    }
    
    @Override
    public PageResult<JobApplication> getApplicationPage(Integer pageNum, Integer pageSize, Integer jobId, Integer status, String orderBy, String orderType) {
        // 计算偏移量
        int offset = (pageNum - 1) * pageSize;
        
        // 默认排序
        if (!StringUtils.hasText(orderBy)) {
            orderBy = "createTime";
        }
        if (!StringUtils.hasText(orderType)) {
            orderType = "desc";
        }
        
        // 查询数据
        List<JobApplication> applications = jobApplicationMapper.selectPage(offset, pageSize, jobId, status, orderBy, orderType);
        
        // 查询总数
        int total = jobApplicationMapper.countApplications(jobId, status);
        
        // 返回分页结果
        return new PageResult<>(pageNum, pageSize, total, applications);
    }
} 